package org.dromara.bykt.service.impl;

import org.dromara.bykt.domain.BYKTCourse;
import org.dromara.bykt.domain.BYKTCourseExercise;
import org.dromara.bykt.domain.BYKTCourseSimulation;
import org.dromara.bykt.mapper.BYKTCourseMapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.bykt.domain.bo.BYKTCourseQuestionsBo;
import org.dromara.bykt.domain.vo.BYKTCourseQuestionsVo;
import org.dromara.bykt.domain.BYKTCourseQuestions;
import org.dromara.bykt.mapper.BYKTCourseQuestionsMapper;
import org.dromara.bykt.service.IBYKTCourseQuestionsService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 刷题Service业务层处理
 *
 * @author Hou
 * @date 2025-06-22
 */
@RequiredArgsConstructor
@Service
public class BYKTCourseQuestionsServiceImpl implements IBYKTCourseQuestionsService {

    private final BYKTCourseQuestionsMapper baseMapper;

    private final BYKTCourseMapper courseMapper;

    /**
     * 查询刷题
     *
     * @param id 主键
     * @return 刷题
     */
    @Override
    public BYKTCourseQuestionsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询刷题列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 刷题分页列表
     */
    @Override
    public TableDataInfo<BYKTCourseQuestionsVo> queryPageList(BYKTCourseQuestionsBo bo, PageQuery pageQuery) {
//        LambdaQueryWrapper<BYKTCourseQuestions> lqw = buildQueryWrapper(bo);
//        Page<BYKTCourseQuestionsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<BYKTCourseQuestionsVo> result = baseMapper.queryPageList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的刷题列表
     *
     * @param bo 查询条件
     * @return 刷题列表
     */
    @Override
    public List<BYKTCourseQuestionsVo> queryList(BYKTCourseQuestionsBo bo) {
        LambdaQueryWrapper<BYKTCourseQuestions> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BYKTCourseQuestions> buildQueryWrapper(BYKTCourseQuestionsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BYKTCourseQuestions> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(BYKTCourseQuestions::getSort);
        if (bo.getCategoryId() != null) {
            List<BYKTCourse> courses = courseMapper.selectList(new LambdaQueryWrapper<BYKTCourse>().eq(BYKTCourse::getCategoryId, bo.getCategoryId()));
            // 提取所有 ID 到 List<Long>
            List<Long> courseIds = courses.stream()
                .map(BYKTCourse::getId)
                .collect(Collectors.toList());
            lqw.in(BYKTCourseQuestions::getCourseId, courseIds);
        }
        lqw.eq(bo.getVideoId() != null, BYKTCourseQuestions::getVideoId, bo.getVideoId());
        lqw.eq(bo.getCourseId() != null, BYKTCourseQuestions::getCourseId, bo.getCourseId());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), BYKTCourseQuestions::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), BYKTCourseQuestions::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getOptions()), BYKTCourseQuestions::getOptions, bo.getOptions());
        lqw.eq(StringUtils.isNotBlank(bo.getAnswer()), BYKTCourseQuestions::getAnswer, bo.getAnswer());
        lqw.eq(StringUtils.isNotBlank(bo.getAnalysis()), BYKTCourseQuestions::getAnalysis, bo.getAnalysis());
        lqw.eq(StringUtils.isNotBlank(bo.getImage()), BYKTCourseQuestions::getImage, bo.getImage());
        lqw.eq(StringUtils.isNotBlank(bo.getQuestionType()), BYKTCourseQuestions::getQuestionType, bo.getQuestionType());
        return lqw;
    }

    /**
     * 新增刷题
     *
     * @param bo 刷题
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(BYKTCourseQuestionsBo bo) {
        bo.setCreateTime(new Date());
        BYKTCourseQuestions add = MapstructUtils.convert(bo, BYKTCourseQuestions.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改刷题
     *
     * @param bo 刷题
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(BYKTCourseQuestionsBo bo) {
        bo.setUpdateTime(new Date());
        BYKTCourseQuestions update = MapstructUtils.convert(bo, BYKTCourseQuestions.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BYKTCourseQuestions entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除刷题信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public BYKTCourseQuestionsVo selectText(String title) {
        return baseMapper.selectVoOne(new LambdaQueryWrapper<BYKTCourseQuestions>().eq(BYKTCourseQuestions::getTitle, title));
    }

    @Override
    public Long queryVideoId(Long videoId) {
        return baseMapper.selectCount(new LambdaQueryWrapper<BYKTCourseQuestions>().eq(BYKTCourseQuestions::getVideoId, videoId));
    }
}
